<?php 
/**
 * 数据库自增自减
 **/
namespace App\Tools;

use App\Models\Order;
use App\Models\ShopInfo;
use App\Models\ShopDish;

class SqlIncrement{
    //订单（自增）
    public function dishIncrement($order_id){
        //验证订单
        try {
            $order = Order::find($order_id);
            if (!$order) {
                return false;
            }
            //店铺销量自增
            ShopInfo::where('shop_id', $order->shop_id)->increment('sales');
            //商品销量自加
            foreach ($order->OrderDetail as $v) {
                ShopDish::find($v->dishes_id)->increment('sales', $v->goods_number);
            }
        }catch (\Exception $e){
            return false;
        }
    }

    //订单（自减）
    public function dishDecrement($order_id){
        try {
            //验证订单
            $order = Order::find($order_id);
            if (!$order) {
                return false;
            }
            //店铺销量自增
            ShopInfo::where('shop_id', $order->shop_id)->decrement('sales');
            //商品销量自加
            foreach ($order->OrderDetail as $v) {
                ShopDish::find($v->dishes_id)->decrement('sales', $v->goods_number);
            }
        }catch (\Exception $e){
            return false;
        }
    }

}

